#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'walkingsky'

from chinese_calendar import is_workday
import datetime,os
from tabulate import tabulate

def rollback_days(n=30):
    '''
    回滚n个交易日的日期
    '''
    i=0
    date = datetime.date.today()
    while i < n:
        date = date + datetime.timedelta(days=-1)
        if is_workday(date) and datetime.datetime.isoweekday(date)<6:
            i = i + 1    
    return date

def print_aligned(df, string_align='left', num_align='right'):
    """根据列类型自动设置对齐方式"""
    alignments = []
    for dtype in df.dtypes:
        if dtype == 'object':  # 字符串列
            alignments.append(string_align)
        else:  # 数字列
            alignments.append(num_align)
    try:
        print(tabulate(df, 
                    headers='keys', 
                    tablefmt='psql', 
                    floatfmt=".4f",
                    colalign=tuple(alignments),
                    showindex=False))
    except (IndexError, ValueError):
        print(df)

def find_files(pattern, directory='.'):
    """
    在指定目录中查找匹配特定模式的文件
    
    参数:
    pattern (str): 要在文件名中查找的字符串，空字符串表示匹配所有文件
    directory (str): 要搜索的目录路径，默认为当前目录
    
    返回:
    list: 匹配的文件路径列表
    """
    matches = []
    for root, dirnames, filenames in os.walk(directory):
        for filename in filenames:
            # 如果模式为空或文件名包含模式，则匹配成功
            if not pattern or pattern in filename:
                matches.append(filename)
    return matches